package com.power.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.Prod;
import com.power.domain.Sku;
import com.power.model.Result;
import com.power.service.ProdService;
import com.power.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 商品管理控制层
 * @Author dingchunlin
 * @Date 2024/07/15 18:44
 * @Version 1.0.0
 */
@Api(tags = "商品接口管理")
@RestController
@RequestMapping("/prod/prod")
public class ProdController {

    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;

    /**
     * 多条件分页查询商品
     * @param current 当前页
     * @param size 每页显示数量
     * @param prodName 商品名称
     * @param status 商品状态
     * @return Result<Page<Prod>>
     */
    @ApiOperation(value = "多条件分页查询商品")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(@RequestParam Long current,
                                            @RequestParam Long size,
                                            @RequestParam(required = false) String prodName,
                                            @RequestParam(required = false) Integer status){
        //多条件分页查询商品
        Page<Prod> page = new Page<Prod>(current, size);
        // 条件查询
        page = prodService.page(page,new LambdaQueryWrapper<Prod>()
                .eq(ObjectUtil.isNotEmpty(status), Prod::getStatus, status)
                .like(ObjectUtil.isNotEmpty(prodName), Prod::getProdName, prodName)
                 .orderByDesc(Prod::getCreateTime)
        );
        return Result.success(page);
    }

    /**
     * 新增商品
     * @param prod 商品实体类
     * @return Result
     */
    @ApiOperation(value = "新增商品")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('prod:prod:save')")
    public Result save(@RequestBody Prod prod){
        Boolean save = prodService.saveProd(prod);
        return Result.handle(save);
    }
    /**
     * 根据商品id查询商品信息
     * @param prodId 商品id
     * @return Result<Prod>
     */
    @ApiOperation(value = "根据标识查询商品详情")
    @GetMapping("/info/{prodId}")
    @PreAuthorize("hasAnyAuthority('prod:prod:info')")
    public Result<Prod> loadProdInfo(@PathVariable("prodId") Long prodId) {
        Prod prod = prodService.loadProdByProdId(prodId);
        return Result.success(prod);
    }

    /**
     * 修改商品信息
     * @param prod 商品实体类
     * @return Result<String>
     */
    @ApiOperation(value = "修改商品信息")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('prod:prod:update')")
    public Result<String> modifyProdInfo(@RequestBody Prod prod) {
        Boolean modified = prodService.modifyProdInfo(prod);
        return Result.handle(modified);
    }

    /**
     * 单个/批量删除商品
     * @param prodIdList 商品id列表
     * @return Result<String>
     */
    @ApiOperation(value = "单个/批量删除商品")
    @DeleteMapping("/{prodIdList}")
    @PreAuthorize("hasAnyAuthority('prod:prod:delete')")
    public Result<String> removeProd(@PathVariable("prodIdList") List<Long> prodIdList) {
        Boolean remove = prodService.removeProdById(prodIdList);
        return Result.handle(remove);
    }

    /**
     * 根据商品id列表查询商品列表
     * @param prodIdList 商品id列表
     * @return  Result<List<Prod>>
     */
    @GetMapping("/getProdListById")
    public Result<List<Prod>> getProdListByIds(@RequestParam List<Long> prodIdList){
        List<Prod> prodList = prodService.listByIds(prodIdList);
        return Result.success(prodList);
    }


    /**
     * 根据商品分类id查询商品列表
     * @param categoryIdList 商品分类id集合
     * @return  Result<List<Prod>>
     */
    @ApiOperation(value = "根据商品分类id集合查询商品列表")
    @GetMapping("/getProdListByCategoryIdList")
    public Result<List<Prod>> loadProdListByCategoryId(@RequestParam List<Long> categoryIdList) {
        List<Prod> prodList = prodService.queryProdListByCategoryIdList(categoryIdList);
        return Result.success(prodList);
    }

    /**
     * 微信小程序端获取商品详情
     * @param prodId 商品id
     * @return Result<Prod>
     */
    @ApiOperation(value = "微信小程序端获取商品详情")
    @GetMapping("/prod/prodInfo")
    public Result<Prod> loadWxProdInfoByProdId(@RequestParam Long prodId) {
        Prod prod = prodService.queryWxProdByProdId(prodId);
        return Result.success(prod);
    }

    /**
     * 根据skuid集合查询商品sku集合
     * @param skuIdList skuid列表
     * @return Result<List<Sku>>
     */
    @ApiOperation(value = "根据skuid集合查询商品sku集合")
    @GetMapping("/getSkuListByIds")
    public Result<List<Sku>> getSkuListByIds(@RequestParam List<Long> skuIdList){
        List<Sku> skus = skuService.listByIds(skuIdList);
        return Result.success(skus);
    }

}
